﻿using BookStore.Solution.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BookStore.Solution.BLL
{
    public class StationeryManager
    {
        private string result = "";
        public List<Product> GetAll()
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "VPP" && a.Status != "Đã xóa"
                           select a).ToList<Product>();
            }
            return product;
        }
        public Product GetbyID(string productID)
        {
            Product product = new Product();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.ProductID == productID && a.Type == "VPP"
                           select a).First<Product>();
            }
            return product;
        }
        public string Insert(Product product)
        {
            using (var context = new BookStoreEntities())
            {
                int numOfRows = context.Products.Where(p => p.ProductID == product.ProductID).Count();
                if (numOfRows == 0)
                {
                    context.Products.Attach(product);
                    context.Entry(product).State = System.Data.Entity.EntityState.Added;
                    if (context.SaveChanges() > 0)
                    {
                        SystemLogManager.Insert("Thêm VPP: " + product);
                        result = "Thêm thành công";
                    }
                    else
                        result = "Thêm thất bại";

                }
                else
                {
                    result = "Mã sản phẩm đã tồn tại trong hệ thống. Vui lòng chọn mã sản phẩm khác.";
                }
            }
            return result;
        }
        public string Update(Product product)
        {
            using (var context = new BookStoreEntities())
            {

                Product nProduct = (from p in context.Products
                                    where p.ProductID == product.ProductID
                                    select p).First();
                nProduct.ProductName = product.ProductName;
                nProduct.CategoryID = product.CategoryID;
                nProduct.Decription = product.Decription;
                nProduct.Unit = product.Unit;
                nProduct.UnitPrice = product.UnitPrice;
                nProduct.Quatity = nProduct.Quatity;
                nProduct.Picture = product.Picture;
                nProduct.Status = product.Status;
                if (context.SaveChanges() > 0)
                {
                    SystemLogManager.Insert("Cập nhật VPP: " + nProduct);
                    result = "Cập nhật thông tin văn phòng phẩm thành công";
                }
                else
                    result = "Cập nhật thông tin văn phòng phẩm thất bại";
            }
            return result;
        }
        public int Delete(string ProductID)
        {
            using (var context = new BookStoreEntities())
            {
                Product nProduct = (from p in context.Products
                                    where p.ProductID == ProductID
                                    select p).First();
                nProduct.Status = "Đã xóa";
                int result = context.SaveChanges();
                if (result > 0)
                    SystemLogManager.Insert("Xóa VPP: " + nProduct);
                return result;
            }
        }
        public List<Product> SearchbyCategory(string categoryID)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                product = (from p in context.Products
                           where p.CategoryID == categoryID && p.Type == "VPP" && p.Status != "Đã xóa"
                           select p).ToList<Product>();
            }
            return product;
        }
        public List<Product> SearchbyID(string productID)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "VPP" && a.ProductID == productID && a.Status != "Đã xóa"
                           select a).ToList<Product>();
            }
            return product;
        }
        public List<Product> SearchbyName(string productName)
        {
            List<Product> product = new List<Product>();
            using (var context = new BookStoreEntities())
            {
                context.Configuration.LazyLoadingEnabled = false;
                product = (from a in context.Products
                           where a.Type == "VPP" && a.ProductName.Contains(productName) && a.Status != "Đã xóa"
                           select a).ToList<Product>();
            }
            return product;
        }
    }
}
